package com.glodon.paas.account.security.cas.ticket;

import com.glodon.paas.account.security.cas.ticket.support.RedisExpirationPolicy;
import org.jasig.cas.authentication.ImmutableAuthentication;
import org.jasig.cas.authentication.principal.SimplePrincipal;
import org.jasig.cas.ticket.TicketGrantingTicketImpl;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
 * @author Don Li
 */
@Ignore
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-config-test.xml")
public class TicketSerializerIT {
    @Autowired
    private RedisTemplate<String, TicketGrantingTicketImpl> template;

    @Autowired
    @Qualifier("tgtExpirationPolicy")
    RedisExpirationPolicy expirationPolicy;

    @Test
    public void test_store_ticket_granting_ticket() {
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new JdkSerializationRedisSerializer());
        ValueOperations<String, TicketGrantingTicketImpl> operations = template.opsForValue();
        operations.set("CAS:TGT:123",
                new TicketGrantingTicketImpl(
                        "123",
                        new ImmutableAuthentication(new SimplePrincipal("user")),
                        expirationPolicy));
        TicketGrantingTicketImpl ticket = operations.get("CAS:TGT:123");
        assertThat(ticket.getId(), is("123"));
        assertThat(ticket.getAuthentication().getPrincipal().getId(), is("user"));
    }

    @After
    public void tearDown() {
        template.delete("CAS:TGT:123");
    }
}
